package com.rs.adminweb.admin.dao.daoImpl;

import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;

import com.jaxplat.commons.Row;
import com.jaxplat.dao.JaxplatSupportDaoImpl;
import com.jaxplat.exceptions.GeneralException;
import com.rs.adminweb.admin.dao.AdminDao;
import com.rs.adminweb.admin.entity.AdminEntity;
import com.rs.util.PagingUtil;

public class AdminDaoImpl extends JaxplatSupportDaoImpl implements AdminDao
{
    private Log log = LogFactory.getLog(AdminDaoImpl.class);

    /**
     * 用户列表
     */
    @SuppressWarnings("rawtypes")
    public Object[] getUserList(Row param) throws GeneralException
    {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from t_admin where 1=1 ");
        if (!"".equals(param.getString("id")) && null != param.getString("id"))
        {
            sql.append("and id =").append(param.getString("id"));
        }
        if (!"".equals(param.getString("loginId")) && null != param.getString("loginId"))
        {
            sql.append(" and login_id ='").append(param.getString("loginId")).append("'");
        }
        int pageNumber = 0;
        int pageSize = 0;
        if (param.getInt("page") != 0)
        {
            pageNumber = param.getInt("pageNumber") * param.getInt("page");
        }
        else
        {
            pageNumber = param.getInt("pageNumber");
        }
        if (param.getInt("rows") != 0)
        {
            pageSize = param.getInt("rows");
        }
        else
        {
            pageSize = param.getInt("pageSize");
        }

        Object[] list = PagingUtil.findRowsForPaging(sql.toString(), new ArrayList(), pageNumber, pageSize);
        return list;
    }

    /**
     * 删除用户
     */
    public int deleteUser(Row row) throws GeneralException
    {
        int count = 0;
        Object ids = row.get("id");
        if (ids instanceof String)
        {
            AdminEntity adminEntity = new AdminEntity();
            adminEntity.setId(Long.decode(ids.toString()));
            count = this.delete(adminEntity);
        }
        else
        {
            for (String id : (String[]) ids)
            {
                AdminEntity adminEntity = new AdminEntity();
                adminEntity.setId(Long.decode(id));
                count = this.delete(adminEntity);
            }
        }
        return count;

    }

    /**
     * 添加用户
     */
    public AdminEntity addUser(AdminEntity adminEntity) throws GeneralException
    {
        return (AdminEntity) this.save(adminEntity);
    }

    @Override
    public int updateUser(AdminEntity adminEntity) throws GeneralException
    {
        // TODO Auto-generated method stub
        return this.update(adminEntity);
    }

    /**
     * 用户登录
     */
    public Row login(AdminEntity adminEntity) throws GeneralException
    {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from t_admin where login_id= '" + adminEntity.getLoginId() + "' and password ='" + adminEntity.getPassword() + "'");
        try
        {
            return this.findRow(sql.toString());
        }
        catch (EmptyResultDataAccessException e)
        {
            return null;
        }
    }

    public Row access(String cert, String date) throws GeneralException
    {
        StringBuffer sql = new StringBuffer();
        sql.append("select t.id_no card_no,doctor.`name` doctor_name,section.`name` section_name, \n");
        sql.append(" (case doctor.pro_title when '1' then '主任医师' when '2' then '副主任医师' end) pro_title \n");
        sql.append("from t_reserve_order t  INNER JOIN t_doctor doctor on t.doctor_id = doctor.id  \n");
        sql.append("INNER JOIN t_section section on doctor.section_id = section.id \n");
        sql.append("where date_format(t.reserve_time,'%Y-%m-%d') ='" + date + "' and t.sms_cert = '" + cert + "'");

        try
        {
            log.info(sql.toString());
            return this.findRow(sql.toString());
        }
        catch (EmptyResultDataAccessException e)
        {
            return null;
        }
    }
}
